package cn.ejiakanghu.logistics.service.impl;

import cn.com.ejiakanghu.core.config.DirectExchangeRabbitMQConfig;
import cn.com.ejiakanghu.core.rabbitmq.DirectExchangeProducer;
import cn.ejiakanghu.logistics.service.TakeOrderService;
import cn.hutool.json.JSONUtil;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.dao.logistics.LogisticsMapper;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallOnlineMapper;
import org.linlinjava.litemall.db.dao.order_manage.BackendStoreMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.wechat_notice.WechatNoticeService;
import org.linlinjava.litemall.db.util.LgBaseInfoStatConstant;
import org.linlinjava.litemall.db.util.ReceiveOrderResultTypeEum;
import org.linlinjava.litemall.db.vo.wechat.ToLitemallBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wj
 * @date 2021/11/4 16:33
 */
@Service
public class TakeOrderServiceImpl implements TakeOrderService {

    @Resource
    private AgedmallLgBaseInfoMapper agedmallLgBaseInfoMapper;

    @Resource
    private AgedmallTakeOrderRecordMapper agedmallTakeOrderRecordMapper;

    @Resource
    private AgedmallServiceOrderMapper agedmallServiceOrderMapper;

    @Resource
    private AgedmallLgMissionMapper agedmallLgMissionMapper;

    @Resource
    private AgedmallDeliveryManInfoMapper agedmallDeliveryManInfoMapper;

    @Resource
    private WechatNoticeService wechatNoticeService;

    @Resource
    private AgedmallStoreMapper agedmallStoreMapper;

    @Resource
    private LogisticsMapper logisticsMapper;

    @Resource
    private AgedmallLgReceiveOrderMapper agedmallLgReceiveOrderMapper;

    @Resource
    private AgedmallLgBusiRecordMapper agedmallLgBusiRecordMapper;

    @Resource
    private LogisticBusiBaseServiceImpl logisticBusiBaseServiceImpl;

    @Resource
    private AgedmallCloudHouseMapper agedmallCloudHouseMapper;

    @Resource
    private AgedmallOnlineMapper agedmallOnlineMapper;

    @Resource
    private DirectExchangeProducer directExchangeProducer;

    @Resource
    private LitemallOrderGoodsMapper litemallOrderGoodsMapper;



    @Override
    public void takeOrderStatChangeWechat(Long recordId) {
        AgedmallTakeOrderRecord takeOrderRecord=agedmallTakeOrderRecordMapper.selectByPrimaryKey(recordId);

        //医生抢单
        if(takeOrderRecord.getMessageType()==4){
            AgedmallServiceOrder agedmallServiceOrder = agedmallServiceOrderMapper.selectByPrimaryKey(takeOrderRecord.getPrintId().longValue());
            agedmallServiceOrder.setDoctorId(takeOrderRecord.getDeliveryManId().longValue());
            agedmallServiceOrder.setStat(3);
            agedmallServiceOrder.setAgreetime(LocalDateTime.now());
            agedmallServiceOrderMapper.updateByPrimaryKeySelective(agedmallServiceOrder);
        }
        else{
            logisticBusiBaseServiceImpl.receiveOrderEvent(takeOrderRecord.getDeliveryManId(),takeOrderRecord.getPrintId(),
                    takeOrderRecord.getMessageType());
        }
    }

    @Override
    public void chooseStoreDliveryMan(Integer id,Integer deliveryManId) {

        //TODO 后台手动分配人员需要操作。。。具体逻辑需要修改
        AgedmallLgMission mission=agedmallLgMissionMapper.selectByPrimaryKey(id);
        AgedmallLgBaseInfo agedmallLgBaseInfo = agedmallLgBaseInfoMapper.selectByPrimaryKey(mission.getBaseInfoId().longValue());
        agedmallLgBaseInfo.setLgStat(LgBaseInfoStatConstant.annoToNowStat(mission.getType()));
        agedmallLgBaseInfoMapper.updateByPrimaryKeySelective(agedmallLgBaseInfo);
        //新增agedmall_lg_receive_order  agedmall_lg_busi_record
        AgedmallLgReceiveOrder receiveOrder = logisticBusiBaseServiceImpl.insertReceiveOrder(agedmallLgBaseInfo, deliveryManId, 2, 1,mission.getId().longValue());
        logisticBusiBaseServiceImpl.insertBusiRecord(mission.getId().longValue(), receiveOrder.getId(), mission.getType(), agedmallLgBaseInfo.getOrderCode(), 1);

    }
    @Override
    public void chooseCloudDliveryMan(Integer id) {

        AgedmallLgMission mission=agedmallLgMissionMapper.selectByPrimaryKey(id);
        AgedmallLgBaseInfo agedmallLgBaseInfo = agedmallLgBaseInfoMapper.selectByPrimaryKey(mission.getBaseInfoId().longValue());
        AgedmallCloudHouse agedmallCloudHouse=agedmallCloudHouseMapper.selectByPrimaryKey(agedmallLgBaseInfo.getStoreId());
        //获取当前门店所有配送人员
        Integer role=missionTypeConversionToDeliveryRole(mission.getType());
        List<AgedmallDeliveryManInfo> deliveryManByStoreIdAndRole = logisticsMapper.getDeliveryManByStoreIdAndRole(agedmallLgBaseInfo.getStoreId().longValue()
                ,role);
        if(deliveryManByStoreIdAndRole.isEmpty()){
            throw new RuntimeException("当前云仓没有配货员");
        }
        chooseStoreDliveryMan(id,deliveryManByStoreIdAndRole.get(0).getId());
    }


    //mission类型转takeOrder的类型
    public int missionTypeConversionToTakeOrderType(Integer missionType){
        switch (missionType){
            case 3:
                return 3;
            case 4:
            case 5:
                return 2;
            case 7:
            case 2:
                return 1;
            case 1:
            case 6:
            case 8:
            default:
                return 0;
        }
    }

    //mission类型转配送员role
    public int missionTypeConversionToDeliveryRole(Integer missionType){
        switch (missionType){
            case 3:
                return 2;
            case 4:
            case 5:
            case 7:
            case 2:
                return 1;
            case 1:
            case 6:
            case 8:
            default:
                return 0;
        }
    }

    //门店配置抢单类型转takeOrder抢单类型
    public int DeliveryRoleConversionToTakeOrderType(Integer role,AgedmallStore store){
        switch (role){
            case 1:
                return store.getDeliveryMode();
            case 2:
                return store.getMaintainMode();
            case 0:
            default:
                return store.getDistributionMode();
        }
    }

    @Override
    public List<AgedmallTakeOrderRecord> addNoticeData(AgedmallLgMission mission,AgedmallLgBaseInfo agedmallLgBaseInfo) {
            //获取当前门店所有配送人员
            Integer role=missionTypeConversionToDeliveryRole(mission.getType());
        List<AgedmallDeliveryManInfo> deliveryManByStoreIdAndRole;
        AgedmallStore store;
        if(agedmallLgBaseInfo.getWarehouseType()==1){
            deliveryManByStoreIdAndRole = logisticsMapper.getDeliveryManByCloudIdAndRole(agedmallLgBaseInfo.getStoreId().longValue()
                    ,role);
            Integer orderGoodsId=logisticBusiBaseServiceImpl.getorderGoodsIds(agedmallLgBaseInfo.getLgContent()).get(0);
            store=logisticsMapper.selectStoreByOrderGoodsId(orderGoodsId);
        }
        else{
            deliveryManByStoreIdAndRole = logisticsMapper.getDeliveryManByStoreIdAndRole(agedmallLgBaseInfo.getStoreId().longValue()
                    ,role);
            store=agedmallStoreMapper.selectByPrimaryKey(agedmallLgBaseInfo.getStoreId().longValue());
        }


            List<Integer> ids=new ArrayList<>();
            deliveryManByStoreIdAndRole.forEach(man -> ids.add(man.getId()));

            Integer messageType=missionTypeConversionToTakeOrderType(mission.getType());

            //抢单类型  0-顺序抢单 1-一起抢单
            //新增TakeOrder抢单表
            return wechatNoticeService.add(mission.getId(), ids, DeliveryRoleConversionToTakeOrderType(role,store), messageType);

    }


    @Override
    public List<AgedmallTakeOrderRecord> addDoctoNoticerData(Long serviceOrderId) {
        //TODO 医生抢单新增TakeOrder相关逻辑
        AgedmallServiceOrder serviceOrder=agedmallServiceOrderMapper.selectByPrimaryKey(serviceOrderId);
        List<AgedmallDoctorInfo> doctorList = agedmallOnlineMapper.selectDoctorListByDeptIdAndOnlineCityId(serviceOrder.getDoctorId().intValue(), serviceOrder.getOnlineCitySerivceId().intValue());
        List<Integer> ids=new ArrayList<>();
        doctorList.forEach(doctor -> ids.add(doctor.getId().intValue()));
       return wechatNoticeService.add(serviceOrder.getId().intValue(), ids, 0, 4);
    }

    @Override
    public void sendToDoctorNotice(Long serviceOrderId) {
        ToLitemallBean toLitemallBean = new ToLitemallBean();
        toLitemallBean.setType(ReceiveOrderResultTypeEum.SEND_DOCTOR_NOTICE.getCode());
        toLitemallBean.setServiceOrderId(serviceOrderId);
        directExchangeProducer.send(JSONUtil.toJsonStr(toLitemallBean), DirectExchangeRabbitMQConfig.TO_LITEMALL_NOTICE);
    }
}
